<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>手写 instanceof</title>
  </head>
  <body>
    <script>
      /**
       * left 是实例化对象
       * right 是构造函数
       * **/
      function myInstanceof(left, right) {
        let proto = Object.getPrototypeOf(left); // 获取实例化对象的原型
        let prototype = right.prototype; // 获取构造函数的原型
        while (true) {
          if (!proto) return false;
          if (proto === prototype) return true;
          proto = Object.getPrototypeOf(proto);
        }
      }

      const prototype = {
        name: 'prototype',
      };
      const obj = Object.create(prototype);

      class Animate {
        eat() {}
      }

      class Cat extends Animate {}

      const cat = new Cat();

      console.log(myInstanceof(cat, Cat));
    </script>
  </body>
</html>
